#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Project ：uitest 
# @Author  : hpc
# @File    : login.py
# @Time    : 2021/6/13 9:23 下午
import requests
from jira import config


class Login(object):

    def jira_login(self, username, password):
        """jira登录-获取cookie
        @param username:jira账号
        @param password:jira密码
        @return:cookies
        """
        session = requests.Session()
        prefix_url = config.URL + "/login.jsp"

        headers = {
            'Content-Type': 'application/x-www-form-urlencoded',
            'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.77 Safari/537.36'
        }
        json = {
            'os_username': username,
            'os_password': password,
            'os_destination': '',
            'user_role': '',
            'atl_token': '',
            'os_cookie': True,
            'login': '登录'
        }
        res = session.post(url=prefix_url, headers=headers, data=json)
        if "您的用户名和密码不正确" in res.text:
            raise Exception("您的用户名和密码不正确")
        print("cookies:-----")
        cookies = "case.editor.user.mode=wysiwyg; "
        for i in session.cookies:
            print(i.name, i.value)
            cookies = cookies + i.name + "=" + i.value + "; "
        return cookies
